﻿using System.Security.Cryptography;
using System.Text;

namespace good1.Core.Security
{
    public class DataProtection
    {
        // Create byte array for additional entropy when using Protect method.
        readonly static byte[] AditionalEntropy = { 9, 8, 7, 6, 5 };
        public static byte[] Protect(string value)
        {
            byte[] data = Encoding.Unicode.GetBytes(value);
            // Encrypt the data using DataProtectionScope.CurrentUser. The result can be decrypted
            //  only by the same current user.
            return ProtectedData.Protect(data, AditionalEntropy, DataProtectionScope.CurrentUser);
        }

        public static string Unprotect(byte[] encryptedValue)
        {
            //Decrypt the data using DataProtectionScope.CurrentUser.
            return Encoding.Unicode.GetString(ProtectedData.Unprotect(encryptedValue, AditionalEntropy, DataProtectionScope.CurrentUser));
        }
    }
}
